import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询列表'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询详情'
});

const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增'
});

const getModifyDesc = formatMessage({
    id: 'api.getModify',
    defaultMessage: '修改'
});

const confirmDesc = formatMessage({
    id: 'api.confirm',
    defaultMessage: '确认'
});

const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});

const getSubmitDesc = formatMessage({
    id: 'api.getSubmit',
    defaultMessage: '提交'
});

const getExcelDesc = formatMessage({
    id: 'api.getExcelDesc',
    defaultMessage: '导出excel'
});

const approveDesc = formatMessage({
    id: 'api.approveDesc',
    defaultMessage: '审核'
});

const approveUploadDesc = formatMessage({
    id: 'api.approveUploadDesc',
    defaultMessage: '上传报告和发票'
});

const approveReportDesc = formatMessage({
    id: 'api.approveReportDesc',
    defaultMessage: '审核报告和发票'
});

const approveFeeDesc = formatMessage({
    id: 'api.approveFeeDesc',
    defaultMessage: '审核发票和费用'
});

const registerDesc = formatMessage({
    id: 'api.registerDesc',
    defaultMessage: '登记核销结果'
});

const dealerSaleInformationsDesc = formatMessage({
    id: 'api.message.dealerSaleInformationsDesc',
    defaultMessage: '获取总代经营权限'
});

const getMarketDepartmentDesc = formatMessage({
    id: 'api.message.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});

const getAreasDesc = formatMessage({
    id: 'api.message.getAreasDesc',
    defaultMessage: '查询国家'
});

const exportDataDesc = formatMessage({
    id: 'api.message.exportDataDesc',
    defaultMessage: '导出'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/marketingActivity/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketingActivities?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/marketingActivities/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/marketingActivities`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 修改
export const modifyOrder = (id, data) => request(`${baseUrl}/marketingActivities/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getModifyDesc));

// 确认
export const confirmData = (id, data) => request(`${baseUrl}/saleProformaInvoices/${id}/confirm`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmDesc));

// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/marketingActivities/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(
        rowVersion
    )
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));

// 提交
export const submitData = (id, rowVersion) => request(`${baseUrl}/marketingActivities/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(
        rowVersion
    )
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getSubmitDesc));

// 审核不通过
export const rejectData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/approveReject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));

// 审核通过
export const approveData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/approvePass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));

// 上传报告和发票
export const approveUploadData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/upload`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveUploadDesc));

// 登记核销结果
export const registerData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/register`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, registerDesc));

// 审核报告和发票通过
export const approveReportData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/approveReport`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveReportDesc));

// 审核报告和发票不通过
export const rejectReportData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/approveReportReject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveReportDesc));

// 审核发票和费用通过
export const approveFeeData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/approveFee`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveFeeDesc));

// 审核发票和费用不通过
export const rejectFeeData = (id, data) => request(`${baseUrl}/marketingActivities/${id}/approveFeeReject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveFeeDesc));

//导出Excel
export const getPiExcel = id => request(`${baseUrl}/marketingActivities/${id}/exportExcel`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, getExcelDesc))
    .then(res => res.data);

export const getDealerSaleInformations = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealerSaleInformations/forSaleProformaInvoice?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, dealerSaleInformationsDesc));
};

// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getAreasSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

export const getAreas = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

// 导出
export const exportData = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketingActivities/export?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDataDesc));
};
